﻿using Masa.Contrib.Dispatcher.Events;
using MasaMES.Contracts.Msm.SysConfig.Commands;
using MasaMES.Services.Msm.Domain.Entities;
using MasaMES.Services.Msm.Domain.Repositories;

namespace MasaMES.Services.Msm.Application.CommandHandlers;

public class SysConfigCommandHandler(ISysConfigRepository sysConfigRepository)
{
    private readonly ISysConfigRepository _sysConfigRepository = sysConfigRepository;

    [EventHandler]
    public async Task CreateHandleAsync(SysConfigCreateCommand command)
    {
        var sysConfigEntity = command.Map<SysConfigEntity>();
        await _sysConfigRepository.AddAsync(sysConfigEntity);
    }

    [EventHandler]
    public async Task UpdateHandleAsync(SysConfigUpdateCommand command)
    {
        var sysConfigEntity = command.Map<SysConfigEntity>();
        await _sysConfigRepository.UpdateAsync(sysConfigEntity);
    }

    [EventHandler]
    public async Task DeleteHandleAsync(SysConfigDeleteCommand command)
    {
        await _sysConfigRepository.RemoveAsync(a => command.Ids.Contains(a.Id));
    }
}
